<template>
  <div class="container">
    <div class="registerForm">
      <img src="../assets/logo_1.png" class="logo" @click="backIndex"/>
      <el-form :model="emailInfo" status-icon :rules="rules2" ref="emailInfo" label-width="100px" class="loginForm">
        <el-form-item  prop="email">
          <el-input type="text"  v-model="emailInfo.email" auto-complete="off" class="loginInput" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item  prop="code" style="">
          <el-input type="code"  v-model="emailInfo.code" auto-complete="off" class="loginInput" placeholder="验证码" style="width:200px;margin-left: 0px;float: left"></el-input>
          <el-button type="primary" @click="sendCode" style="margin-left: 10px" :disabled="val">{{ send }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="loginBtn" @click="submitForm('emailInfo')" >登录</el-button>
        </el-form-item>
      </el-form>

      <span style="margin:13px 0px 0px 10px;display: inline-block;float: left;color: rgba(0, 0, 0, 0.5)" @click="changeLogin"> 换种方式登陆 ？ </span>
      <div class="footer-tip" @click="toRegister" style="display: inline-block;float: right">


        没有账号？直接注册
      </div>
    </div>
    <!-- <img class="bg_bottom" src="../assets/bg_bottom.png"/>
   <img class="bg_bottom2" src="../assets/bg_bottom2.png"/> -->
  </div>
</template>

<style>
  @import "../assets/Animate/animate.min.css";

  body {
    padding: 0;
    margin: 0;
  }

  * {
    box-sizing: border-box;
  }

  .container {
    width: 100%;
    position: relative;
    height: 100%;
    overflow: hidden;
  }

  .container::before {
    content: '';
    position: absolute;
    background-image: url("../assets/login.jpg");
    background-size: 100% 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    transform: matrix(1, 0, 0, 1, 0, 0);
    transition: all 500ms linear 0s;
  }

  .container:hover::before {
    transform: matrix(1.05, 0, 0, 1.05, 3.07, 5.7)
  }

  .registerForm {
    background: #fff;
    border: 1px solid #ededed;
    width: 450px;
    height: 320px;
    margin: 250px auto 250px auto;
    box-shadow: 0px 5px 8px #888;
    border-radius: 8px;
    position: relative;
  }

  .loginForm {
    padding: 0 36px;
  }

  .el-form-item__content {
    margin-left: 0px !important;
  }

  .loginInput {
    width: 100%;
  }

  .footer-tip {
    color: rgba(0, 0, 0, 0.5);
    font-size: 16px;
    cursor: pointer;
    position: absolute;
    bottom: 16px;
    right: 16px;
  }

  .bg_bottom {
    position: fixed;
    bottom: 0;
    right: 0;
  }

  .bg_bottom2 {
    position: fixed;
    bottom: 0;
    left: 0;
  }

  .logo {
    width: 150px;
    height: 80px;;
    cursor: pointer;
    opacity: 0.7;
  }

  .loginBtn {
    width: 100%;
  }

</style>

<script>/* eslint-disable indent */

import fetch from '../api/fetch'

export default {
  data () {
    var validUsername = (rule, value, callback) => {
      var regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/

      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else if(!regEmail.test(value)){
       callback(new Error('请输入正确的邮箱'))
      }else{
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      emailInfo: {
        email: '',
        code: ''
      },
      send:'发送验证码',
      totalTime:10,
      flag:null,
      val:false,
      rules2: {
        email: [{validator: validUsername, trigger: 'blur'}],
        code: [{validator: validatePass, trigger: 'blur'},
            {max:6,min:6 ,message:'验证码长度为6位',trigger: 'blur'}]
      }
    }
  },
  mounted() {
    this.addAnimation()
  },
  methods: {


    /*更换登陆方式方法*/
    changeLogin(){
      this.$router.push("/login")
    },

    sendCode(){
      if(!this.flag){
        // this.flag = !this.flag
        this.flag = window.setInterval(() => {
          if(this.totalTime >0) {
            this.totalTime--
            this.send = this.totalTime + 's后再发送'
            this.val = true
          } else {
            this.send = "发送验证码"
            clearInterval(this.flag)
            this.flag = null
            this.val = false
          }
        },1000)

        if(this.totalTime > 0 && this.totalTime <= 10){
          this.$http.post("http://localhost:9000/apigateway/email/send",JSON.stringify(this.emailInfo),{headers:{'Content-Type': 'application/json'}})
        }
      }

      if(this.totalTime === 0 ) {
        this.send = '发送验证码'
        this.totalTime = 10
      }




    },

    addAnimation() {
      let form = document.getElementsByClassName('registerForm')[0];
      form.classList.add('animated')
      form.classList.add('bounceInDown')
    },
    backIndex () {
      this.$router.push({name: 'index'})
    },
    toRegister () {
      this.$router.push({name: 'register'})
    },
    submitForm (emailInfo) {
      this.$refs[emailInfo].validate(valid => {
        if (valid) {

          this.$http.post("http://localhost:9000/apigateway/email/verify",JSON.stringify(this.emailInfo),{headers:{'Content-Type': 'application/json'}}).then(res => {
           if(res.data.data === true){
             this.$http.post("http://localhost:9000/apigateway/user/emailLogin",JSON.stringify(this.emailInfo),{headers:{'Content-Type': 'application/json'}})
               .then(res => {
                 if (res.status === 200) {
                   if (res.data.code === 0) {
                     localStorage.setItem('token', res.data.msg)
                     localStorage.setItem('companyId', res.data.data.companyId)
                     localStorage.setItem('role', res.data.data.type)
                     sessionStorage.setItem('userId', res.data.data.id)
                     if (res.data.data.type === 1) {
                       this.$router.push({name: 'userInfo', params: {refresh: 1}})
                     } else {
                       this.$router.push({name: 'hrView', params: {hrRefresh: 2}})
                     }
                   }
                 }
               })
               .catch(e => {
                 this.$message({
                   message: '邮箱或者验证码错误',
                   type: 'warning'
                 })
                 console.log(e)
               })
           }
          })
           }
          })



    }
  }
}
</script>
